package com.wireMockEurekaServer.WireMockEurekaServer.controller;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.tomakehurst.wiremock.WireMockServer;
@RestController
public class MockController {
	@Autowired
	RestTemplate restTemplate;
	
	@Autowired
	WireMockServer wireMockServer;
	
	@Value("${wireMock.address}")
	private String wireMockAddress;
	
	@Value("${wireMock.port}")
	private String wireMockPort;
	
	private static boolean isProxy = false;
	
	@RequestMapping(value = "/**", method = RequestMethod.GET)
	public Object getAll(HttpServletRequest httpServletRequest ,@RequestParam Map<String, Object> requestMap) {
		StringBuffer mocoUrl = new StringBuffer();
		mocoUrl.append("http://").append(wireMockAddress).append(":")
				.append(wireMockPort).append(httpServletRequest.getServletPath());
		if(!requestMap.isEmpty()){
			mocoUrl.append("?");
			boolean first = true;
			for(String key:requestMap.keySet()){
				if(!first){
					mocoUrl.append("&");
				}
				first = false;
				mocoUrl.append(key).append("={").append(key).append("}");
			}
		}
		String jsonStr = restTemplate.getForEntity(mocoUrl.toString(), String.class,requestMap).getBody();
		Object object = JSONObject.parse(jsonStr);
		return object;
	}
	
	@RequestMapping(value = "/**", method = RequestMethod.POST)
	public Object postAll(HttpServletRequest httpServletRequest ,@RequestBody Object requestBody) {
		String requestJsonStr = JSON.toJSONString(requestBody);
		StringBuffer mocoUrl = new StringBuffer();
		mocoUrl.append("http://").append(wireMockAddress).append(":")
				.append(wireMockPort).append(httpServletRequest.getServletPath());
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
//		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		HttpEntity<String> formEntity = new HttpEntity<String>(requestJsonStr, headers);
		String jsonStr = restTemplate.postForEntity(mocoUrl.toString(), formEntity,String.class).getBody();
		Object object = JSONObject.parse(jsonStr);
		return object;
	}
	
	@RequestMapping(value = "/proxyOn" , method = RequestMethod.GET)
	public String proxyOn(@RequestParam String proxyPath){
		if(isProxy){
			wireMockServer.stopRecording();
		}
		isProxy = true;
		//开启录制
		wireMockServer.startRecording(proxyPath);
		return "Proxy is on! The proxyPath is:" + proxyPath;
	}
	
	@RequestMapping(value = "/proxyOff",method = RequestMethod.GET)
	public String proxyOff(){
		if(isProxy){
			//关闭录制
			wireMockServer.stopRecording();
		}
		isProxy = false;
		return "Proxy is off!";
	}
}
